//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    /****banner */
    fenlei: app.globalData.fenlei,
    cunarr: [],
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    logo: false,     //登录框
    movies: [
   
    ],
    fenlei: [
      { pic: '../../images/mobile.png', title: '移动宽带', url:'../fenlei/fenlei'},
      { pic: '../../images/unicom.png', title: '联通宽带', url: '../fenlei/fenlei' },
      { pic: '../../images/telecom.png', title: '电信宽带', url: '../fenlei/fenlei' },
      { pic: '../../images/qita.png', title: '分销中心', url: '../distribution/distribution' },      
    ],
   
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
  
  },  
  fenlei: function (e) {    
    console.log(e)
    var url = e.currentTarget.dataset.url;
    var id = e.currentTarget.dataset.id;
    wx.setStorageSync("state", "success") //存一个值
    wx.setStorageSync("fenlei", id)
    console.log(id)
    app.swtTa(url)
    app.navTo(url)    
  },
  leifenlei: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.setStorageSync("state", "success") //存一个值
    wx.setStorageSync("fenlei", id)
    console.log(id)
    app.swtTa('../fenlei/fenlei')  
  },
  kdyy: function (e) {
   
    app.navTo('../yuyue/yuyue?id=' + e.currentTarget.dataset.id) 
  },
  item:function(e){

    app.navTo('../dingdan/dingdan?id=' + e.currentTarget.dataset.id) 
  },
  upper: function (e) {
    console.log(e)
  },
  lower: function (e) {
    console.log(e)
  },
  scroll: function (e) {
    console.log(e)
  },
  scrollToTop: function (e) {
    this.setAction({
      scrollTop: 0
    })
  },
  tap: function (e) {
    for (var i = 0; i < order.length; ++i) {
      if (order[i] === this.data.toView) {
        this.setData({
          toView: order[i + 1],
          scrollTop: (i + 1) * 200
        })
        break
      }
    }
  },
  tapMove: function (e) {
    this.setData({
      scrollTop: this.data.scrollTop + 10
    })
  },
  //购买
  buy:function(e){
    app.navTo('../buy/buy?id=' + e.currentTarget.dataset.id)  
  },
    //获取微信
  getUserInfo: function (e) {
    var that = this;
    wx.showLoading({
      title: '请稍后',
      success: function () {
        wx.login({
          success: function (res) {
            wx.hideLoading();
            if (!e.detail.rawData) {
              wx.showModal({
                title: '提示',
                content: '请重新点微信一键登录',
              })
              return false;
            }
            wx.request({
              url: app.appUrl + '/User/logins',
              method: 'POST',
              data: {
                utoken: '',
                code: res.code,
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv,
                bid: 18
              },
              success: function (rs) {

                if (rs.data.success == 10) {

                  wx.request({
                    url: app.appUrl + '/User/getUid',
                    data: {
                      openid: rs.data.openid
                    },
                    method: 'POST',
                    success: function (ress) {
                      if (ress.data.data) {
                        if (that.data.uid) {
                          wx.request({
                            url: app.appUrl + '/user/fenxiao',
                            data: {
                              xuid: ress.data.data,
                              ouid: that.data.uid,
                              bid: wx.getStorageSync('bid')
                            },
                            success: function (e) {
                              console.log('分销关系', e);
                            }
                          })
                        }
                        wx.setStorageSync('userinfo', {
                          'oepnid': rs.data.openid,
                          'uid': ress.data.data,
                          'nickname': e.detail.userInfo.nickName,
                          'head': e.detail.userInfo.avatarUrl,
                        })
                        wx.setStorageSync('bid', 18)
                        wx.setStorageSync('uid', ress.data.data)
                        that.setData({
                          logo: false,
                        })
                        wx.showTabBar({
                          
                        })
                        wx.hideLoading()
                      } else {
                        wx.hideLoading()
                        wx.showToast({
                          title: '请重试',
                          icon: 'none'
                        })
                      }
                    }
                  })
                } else {
                  wx.hideLoading()
                  wx.showToast({
                    title: '请重试',
                    icon: 'none'
                  })
                }
              },
              fail: function () {
                wx.hideLoading()
                wx.showToast({
                  title: '请重试',
                  icon: 'none'
                })
              }
            })
          }
        })
      }
    })

  },
  onLoad: function (options){
   
    var that = this;
    if (options.uid) {
      that.setData({
        uid: options.uid
      });
    }
    wx.getStorageSync('userinfo') ? that.setData({
      logo: false
    }) : that.showmemu();

  },
  showmemu:function(){
    var that = this;
    that.setData({
      logo: true
    });
    wx.hideTabBar({
      
    })
  },
  onShow:function(){
    var that=this;
    /**
     * 轮播图
     */
    wx.request({
      url: app.appUrl +'/banner/banlist',
      data:{
        bid:18
      },
      success:function(e){
        console.log(e);
       that.setData({
         movies: e.data.data
       })
      }
    })
    /**
     * 广告图
     */
    wx.request({
      url: app.appUrl+'/goods/menuimg',
      data:{
        bid:18
      },
      success:function(res){
       
       that.setData({
         menuimg:res.data.data
       })
      }
    })
    /**
     * 商品信息
     */
    wx.request({
      url: app.appUrl+'/goods/menu',
      data:{
        bid:18
      },
      success:function(res){
        console.log('goods',res.data.data)
        that.setData({
          goods:res.data.data
        })
      }
    })
  }
})